#include "InvTransform.h"

void InvTransform(uint8* SrcImage, uint8* dstImage, Matrix* TransformMat, uint8 gray, size_t row, size_t col)
{
    for (size_t y = 0; y < row; y++)
    {
        for (size_t x = 0; x < col; x++)
        {
            float vectordata[3] = { x,y,1.f };
            Matrix vector = { vectordata ,3,1 };

            float vecResdata[3] = { 0.f,0.f,0.f };
            Matrix vectorres = { vecResdata ,3,1 };

            MatrixMultiply(TransformMat, &vector, &vectorres);

            float Newx = vecResdata[0] / vecResdata[2];
            float Newy = vecResdata[1] / vecResdata[2];

            if (Newx >= 0.f && Newx < col && Newy >= 0.f && Newy < row)
            {
                dstImage[y * col + x] = SrcImage[(int) (Newy) *col + (int) (Newx)];
            }
            else
            {
                dstImage[y * col + x] = gray;
            }
        }
    }

}
